package 栈;

import java.util.Stack;

public class Solution2_test {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        //遍历数组
        for(int i = 0;i<s.length();i++){
            char ch1 = s.charAt(i);
            //判断是否是左括号
            if(ch1=='('||ch1=='['||ch1=='{'){
                stack.push(ch1);
            }else{
                //是右括号
                if(stack.isEmpty()){
                    //栈为空,所以没有可以匹配的左括号,返回false
                    return false;
                }
                char ch2 = stack.peek();
                if(ch2=='('&&ch1==')'||ch2=='['&&ch1==']'||ch2=='{'&&ch1=='}'){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(!stack.isEmpty()){
            return false;
        }
        return true;
    }
}
